/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import net.octopeer.core.Peer;
import net.octopeer.core.PeerNode;
import net.octopeer.core.Neighbor;
import net.octopeer.core.Resource;
import net.octopeer.message.Request;
import net.octopeer.network.Talker;

/**
 *
 * @author andria
 */
public class OctopeerUI extends Thread {
    OctopeerService oService;
    Peer peer;
    
    public OctopeerUI(Peer peer, OctopeerService oService) {
        this.oService = oService;
        this.peer = peer;
    }
    
    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader stdin = new BufferedReader(isr);
            
            while (true) {
                String command = null;
                String argument = null;
                String[] tokens = null;

                System.out.print("Octopeer> " );
                String input = stdin.readLine();
                if (input == null ) {
                    System.out.println("Commmand:");
                    System.out.println("\tresource [add|del|list] [resourceType] [resourceDetail]");
                    System.out.println("\tneighbor [add|del|list|ping] [neighborIP]");
                    System.out.println("\tdiscover [resourceType] [resourceDetail|resourceKeyword]");
                    System.out.println("\texit");
                } else {
                    tokens = input.split(" ");

                    if (tokens.length == 0) {
                        System.out.println("Commmand:");
                        System.out.println("\tresource [add|del|list] [resourceType] [resourceDetail]");
                        System.out.println("\tneighbor [add|del|list|ping] [neighborIP]");
                        System.out.println("\tdiscover [resourceType] [resourceDetail|resourceKeyword]");
                        System.out.println("\texit");
                    } else {
                        command = tokens[0];
                        if (command.equalsIgnoreCase("exit")) {
                            break;
                        } else {
                            evaluateCommand(tokens);
                        }
                    }
                }
            }
        } catch(Exception e) {
            System.out.print(e);
        }
    }
    
    private void evaluateCommand(String[] tokens) throws Exception {
        if (tokens[0].equalsIgnoreCase("test")) {
            testService(tokens);
        } else if (tokens[0].equalsIgnoreCase("resource")) {
            resourceService(tokens);
        } else if (tokens[0].equalsIgnoreCase("neighbor")) {
            neighborService(tokens);
        } else if (tokens[0].equalsIgnoreCase("discover")) {
            discoverService(tokens);
        } else {
            System.out.println("Commmand:");
            System.out.println("\tresource [add|del|list] [resourceType] [resourceDetail]");
            System.out.println("\tneighbor [add|del|list|ping] [neighborIP]");
            System.out.println("\tdiscover [resourceType] [resourceDetail|resourceKeyword]");
            System.out.println("\texit");
        }
    }

    private void discoverService(String[] tokens) throws Exception {
        int neighLoop = 0;
        String reqString = "";
        while (neighLoop < this.peer.getNeighborList().size()) {
            PeerNode neighPeer = (PeerNode) this.peer.getNeighborList().get(neighLoop).getNode();
            // send request string to requestService
            Request req = new Request(tokens[1], tokens[2], this.peer.getNode(), 8, neighPeer);
            reqString = "request " + req.toXMLString();
            oService.forwardService(neighPeer, reqString);
            neighLoop++;
        }
    }

    private void neighborService(String[] tokens) throws Exception {
        PeerNode peerNode = new PeerNode(tokens[2], Integer.valueOf(tokens[3]));
        
        if (tokens[1].equals("add")) {
            oService.neighborAddService(peerNode);
        } else if (tokens[1].equals("del")) {
            oService.neighborDelService(peerNode);
        } else if (tokens[1].equals("list")) {
            oService.neighborListService();
        }
    }

    private void resourceService(String[] tokens) throws Exception {
        
        if (tokens[1].equals("add")) {
            Resource res = new Resource(tokens[2], tokens[3]);
            oService.resourceAddService(res);
        } else if (tokens[1].equals("del")) {
            Resource res = new Resource(tokens[2], tokens[3]);
            oService.resourceDelService(res);
        } else if (tokens[1].equals("list")) {
            oService.resourceListService();
        }
    }
    
    private void testService(String[] tokens) throws Exception {
        int neighborLoop = 0;
        
        while (neighborLoop < peer.getNeighborList().size()) {
            PeerNode neighborNode = (PeerNode) peer.getNeighborList().get(neighborLoop).getNode();
            Talker t = new Talker(neighborNode.getNodeIPaddress(), neighborNode.getNodePort());
            t.sendMessage("Test peer[" + neighborNode.getNodeIPaddress() + ":"+ 
                    neighborNode.getNodePort() + "]=>" +tokens[1]);
            neighborLoop++;
        }
    }

//    public static void main(String[] args) {
//        OctopeerService oServ = new OctopeerService();
//        OctopeerUI oUi = new OctopeerUI(oServ);
//        oUi.start();
//        
//    }

}
